package com.qolome.order.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qolome.enums.OrderStatusEnum;
import com.qolome.enums.PayStatusEnum;
import com.qolome.order.dto.request.CartRequestDto;
import com.qolome.order.dto.request.OrderRequestDto;
import com.qolome.order.model.OrderDetail;
import com.qolome.order.model.OrderMaster;
import com.qolome.order.repository.OrderDetailRepository;
import com.qolome.order.repository.OrderMasterRepository;
import com.qolome.order.service.OrderService;
import com.qolome.product.dto.request.CartDTO;
import com.qolome.product.dto.response.ProductInfoResponseDto;
import com.qolome.product.feign.ProductFeign;
import com.qolome.utils.KeyUtil;

/**
 * @author Qolome
 * @version V1.0
 * @Description 订单
 * @date 2020年 01月20日 10:21:32
 */
@Service
public class OrderServiceImpl implements OrderService {

	/*
	 * @Autowired private OrderDetailRepository orderDetailRepository;
	 */

    @Autowired
    private OrderMasterRepository orderMasterRepository;
    
    @Autowired
    private OrderDetailRepository orderDetailRepository;
    
    @Autowired
    private ProductFeign productFeign;

    

    @Override
    public OrderRequestDto create(OrderRequestDto orderDTO) {
    	 String orderId = KeyUtil.genUniqueKey();
        // 查询商品信息(调用商品服务)
    	 //产品信息
    	List<ProductInfoResponseDto> productInfoList = productFeign.listForOrder(orderDTO.getCartRequestDtoList().stream().map(e->
    	e.getProductId()).collect(Collectors.toList()));
        // 计算总价
        BigDecimal orderAmout = new BigDecimal(BigInteger.ZERO);
        
        
        for (CartRequestDto cartRequestDto: orderDTO.getCartRequestDtoList()) {
        	
        	
            for (ProductInfoResponseDto productInfo: productInfoList) {
                if (productInfo.getProductId().equals(cartRequestDto.getProductId())) {
                    //单价*数量
                    orderAmout = productInfo.getProductPrice()
                            .multiply(new BigDecimal(cartRequestDto.getProductQuantity()))
                            .add(orderAmout);
                    
                    OrderDetail orderDetail = new OrderDetail();
                    BeanUtils.copyProperties(productInfo, orderDetail);
                    orderDetail.setDetailId(KeyUtil.genUniqueKey());
                    orderDetail.setOrderId(orderId);
                    orderDetail.setProductQuantity(cartRequestDto.getProductQuantity());
                    //订单详情入库
                    orderDetailRepository.save(orderDetail);
                }
            }
        }

       //扣库存(调用商品服务)
        List<CartDTO> cartDTOList = orderDTO.getCartRequestDtoList().stream()
                .map(e -> new CartDTO(e.getProductId(), e.getProductQuantity()))
                .collect(Collectors.toList());
        productFeign.decreaseStock(cartDTOList);

        
        //订单入库
        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setOrderId(KeyUtil.genUniqueKey());
        BeanUtils.copyProperties(orderDTO, orderMaster);
        orderMaster.setOrderAmount(orderAmout);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());

        orderMasterRepository.save(orderMaster);
        return orderDTO;
    }
}
